﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_InterviewRequestBillId_FormItem" prop="Entity.InterviewRequestBillId">
								<template #label>
									<WtmLabelTooltip label="面试表" tooltip="面试表" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.InterviewRequestBillId" clearable class="width100" :disabled="isEntityIdFromExternal">
									<el-option
										v-for="item in stateData.AllSysHrInterviewRequestBills"
										:key="item.Value"
										:value="item.Value"
										:label="item.Text"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_HrStatus_FormItem"
								prop="Entity.HrStatus"
								:rules="[{ required: true, message: '状态为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="状态" tooltip="单据状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.HrStatus" clearable class="width100">
									<el-option v-for="option in HR_STATUS_OPTIONS" :key="option.value" :value="option.value" :label="option.label"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, nextTick, type ComponentInternalInstance } from 'vue';
import { SysHrInterviewPoolApi as api } from '/@/views/hr/syshrinterviewpool/api';
import other from '/@/utils/other';
import { ISysHrInterviewPoolVm } from '/@/views/hr/syshrinterviewpool/api/interface';
import { IViewModel } from '/@/types/common';

// 常量定义
const HR_STATUS_OPTIONS = [
	{ value: '申请中', label: '申请中' },
	{ value: '已审核', label: '已审核' },
	{ value: '已拒绝', label: '已拒绝' },
	{ value: '已面试', label: '已面试' },
	{ value: '面试通过', label: '面试通过' },
	{ value: '面试未通过', label: '面试未通过' },
	{ value: '体检中', label: '体检中' },
	{ value: '体检通过', label: '体检通过' },
	{ value: '体检未通过', label: '体检未通过' },
	{ value: '背调中', label: '背调中' },
	{ value: '背调通过', label: '背调通过' },
	{ value: '背调未通过', label: '背调未通过' },
	{ value: 'Offer已发', label: 'Offer已发' },
	{ value: 'Offer已接受', label: 'Offer已接受' },
	{ value: 'Offer已拒绝', label: 'Offer已拒绝' },
	{ value: '已录用', label: '已录用' },
	{ value: '已入职', label: '已入职' },
	{ value: '已报到', label: '已报到' },
	{ value: '入职资料待完善', label: '入职资料待完善' },
	{ value: '试用中', label: '试用中' },
	{ value: '已转正', label: '已转正' },
	{ value: '已放弃', label: '已放弃' },
	{ value: '已淘汰', label: '已淘汰' },
	{ value: '离职办理中', label: '离职办理中' },
	{ value: '已离职', label: '已离职' },
] as const;

const BUTTON_TEXTS = {
	SUBMIT: '提交',
	CLOSE: '关闭',
} as const;

const API_ENDPOINTS = {
	BILLS: '/api/SysHrInterviewPool/GetSysHrInterviewRequestBills',
} as const;

const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const isEntityIdFromExternal = ref(false);

// 初始化表单数据
const stateData = reactive<IViewModel<ISysHrInterviewPoolVm>>({
	vmModel: {
		Entity: {
			InterviewRequestBillId: null,
			HrStatus: 0,
		},
	},
	AllSysHrInterviewRequestBills: [],
});

// 统一的成功处理函数
const handleSuccess = () => {
	ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
	emit('refresh');
	loading.value = false;
	closeDialog();
};

// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api().add(stateData.vmModel).then(handleSuccess).catch(handleError);
		}
	});
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};

// 加载页面数据
const loadPageData = async () => {
	const [bills] = await Promise.allSettled([other.getSelectList(API_ENDPOINTS.BILLS, [], false)]);

	// 处理成功的结果
	if (bills.status === 'fulfilled') stateData.AllSysHrInterviewRequestBills = bills.value;
};

// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		await loadPageData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SUBMIT,
			type: 'primary',
			onClick: onSubmit,
			visible: true,
		},
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
